﻿using fyiReporting.RDL;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace PcaReportes
{
    public class RdlReport
    {
        private byte[] _bytesPdf;
        private RDLParser rdlp;
        private Report r;

        private string GetSource(string file)
        {
            StreamReader fs = null;
            string prog = null;
            try
            {
                fs = new StreamReader(file);
                prog = fs.ReadToEnd();
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            return prog;
        }

        private Report GetReport(string prog, string file)
        {
            try
            {
                //Leer el archivo de configuracion "RdlEngineConfig.xml"
                string searchDir = HttpContext.Current.Server.MapPath("~/Bin") + Path.DirectorySeparatorChar;

                RdlEngineConfig.RdlEngineConfigInit(searchDir);

                rdlp = new RDLParser(prog);
                string folder = Path.GetDirectoryName(file);
                if (folder == "")
                    folder = Environment.CurrentDirectory;
                rdlp.Folder = folder;
                //rdlp.DataSourceReferencePassword = new NeedPassword(this.GetPassword);

                r = rdlp.Parse();
                if (r.ErrorMaxSeverity > 0)
                {
                    if (r.ErrorMaxSeverity >= 8)
                        r = null;
                    r.ErrorReset();
                }

                if (r != null)
                {
                    r.Folder = folder;
                    r.Name = Path.GetFileNameWithoutExtension(file);
                    //r.GetDataSourceReferencePassword = new RDL.NeedPassword(GetPassword);

                    return r;
                }
                else
                {
                    throw new Exception("No se pudo procesar el reporte..."); 
                }
                
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public RdlReport(string pathRpt,ListDictionary parametros)
        {
            string source = GetSource(pathRpt);
            Report report = GetReport(source, pathRpt);
            report.RunGetData(parametros);
            MemoryStreamGen sg = null;
            sg = new MemoryStreamGen("", null, "pdf");
            report.RunRender(sg, OutputPresentationType.PDF, "");
            MemoryStream ms = sg.MemoryList[0] as MemoryStream;
            _bytesPdf = ms.ToArray();
        }

        public RdlReport(string pathRpt) : this(pathRpt, new ListDictionary()) { }

        public string GetNombreReporte
        {
            get
            {
                return r.Name;
            }
        }

        public byte[] GetBytesPdf
        {
            get {
                return _bytesPdf; 
            }
        }
    }
}
